package com.lyn.solitude.pluginhost

import android.app.Application
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Process
import android.util.Log
import com.lyn.solitude.api.PluginHostAidl
import com.lyn.solitude.api.PluginListenerAidl
import com.lyn.solitude.entity.Plugin
import com.lyn.solitude.pluginhost.manager.AndServerManager
import java.util.concurrent.TimeUnit


/**
 * @author longyn 2024/5/12
 * @version 1.0.0
 */
object PluginManager {

    private const val TAG = "PluginManager"

    @JvmStatic
    lateinit var application: Application

    @JvmStatic
    var pluginHostAidl: PluginHostAidl? = null

    @JvmStatic
    var pluginListenerAidl: PluginListenerAidl? =null

    @JvmStatic
    fun initialize(application: Application) {
        if (this::application.isInitialized) {
            return
        }
        this.application = application
        AndServerManager.initialize(application)
    }

    /**
     * 设置插件监听器
     */
    @JvmStatic
    fun setPluginListener(pluginListenerAidl: PluginListenerAidl) {
        this.pluginListenerAidl = pluginListenerAidl
        pluginHostAidl?.setPluginListenerAidl(pluginListenerAidl)
    }

    /**
     * 获取插件进程id
     */
    @JvmStatic
    fun getPid(): Int {
        pluginHostAidl?.let {
            return it.pid
        }
        return -1
    }

    /**
     * 安装插件
     * @param path 插件路径
     */

    @JvmStatic
    fun install(path: String): Boolean {
        try {
            return pluginHostAidl?.install(path) == true
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        Log.d(TAG, "安装失败")
        return false
    }

    /**
     * 卸载插件
     * @param id 插件id
     */
    @JvmStatic
    fun uninstall(id: Long): Boolean {
        try {
            return pluginHostAidl?.unInstall(id) == true
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        Log.d(TAG, "卸载失败")
        return false
    }

    /**
     * 获取插件
     * @param id 插件id
     */
    @JvmStatic
    fun getPlugin(id: Long): Plugin? {
        try {
            pluginHostAidl?.getPlugin(id)?.let {
                return Plugin.toPlugin(it)
            }
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return null
    }

    /**
     * 获取全部插件
     * @return 插件列表
     */
    @JvmStatic
    fun getAllPlugin(): MutableList<Plugin>? {
        try {
            pluginHostAidl?.allPlugin?.let {
                return Plugin.toListPlugin(it)
            }
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return mutableListOf()
    }

    /**
     * 获取插件logo
     * @param id 插件id
     */
    @JvmStatic
    fun getLogo(id: Long): Bitmap? {
        try {
            pluginHostAidl?.getLogo(id)?.let {
                return BitmapFactory.decodeByteArray(it, 0, it.size)
            }
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return null
    }

    /**
     * 打开插件
     * @param id 插件id
     */
    @JvmStatic
    fun open(id: Long): Boolean {
        try {
            return pluginHostAidl?.open(id) == true
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return false
    }

    /**
     * 跳转插件
     * @param id 插件id
     * @param className 跳转类名
     */
    @JvmStatic
    fun jump(id: Long, className: String?): Boolean {
        try {
            return pluginHostAidl?.jump(id, className) == true
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return false
    }
}